﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI;
using YJKRevitDimensionText.DoorWindowDim;
using YJKRevitView;

namespace YJKRevitDimensionText.Commands
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdDoorWindowDimension : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			if (Convert.ToInt32(cmdData.Application.Application.VersionNumber) < 2016)
			{
				MessageBox.Show("此功能只支持Revit2016及其以上版本");
				return Autodesk.Revit.UI.Result.Succeeded;
			}
            CmdViewType cmdViewType = (CmdViewType)96;
			if (SwitchView.SetUsableView(cmdViewType, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			CmdDoorWindowDimension.HideWarningKit @object = new CmdDoorWindowDimension.HideWarningKit("Modify Autodesk.Revit.DB.View Underlay");
			cmdData.Application.Application.FailuresProcessing += @object.OnFailuresProcessing;
			CmdDoorWindowDimension.HideWarningKit object2 = new CmdDoorWindowDimension.HideWarningKit("DeleteSplitcv and GetOutWallCv");
			cmdData.Application.Application.FailuresProcessing += object2.OnFailuresProcessing;
			CmdDoorWindowDimension.HideWarningKit object3 = new CmdDoorWindowDimension.HideWarningKit("Restore Autodesk.Revit.DB.View Underlay");
			cmdData.Application.Application.FailuresProcessing += object3.OnFailuresProcessing;
			CmdDoorWindowDimension.HideWarningKit object4 = new CmdDoorWindowDimension.HideWarningKit("get room data");
			cmdData.Application.Application.FailuresProcessing += object4.OnFailuresProcessing;
			new DoorWindowDimension(cmdData).CreateElementDimension();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Document m_doc;

		public class HideWarningKit
		{
			public HideWarningKit(string transactionName)
			{
				this.m_TransactionName = transactionName;
			}

			public void OnFailuresProcessing(object sender, FailuresProcessingEventArgs e)
			{
				FailuresAccessor failuresAccessor = e.GetFailuresAccessor();
				string transactionName = failuresAccessor.GetTransactionName();
				IList<FailureMessageAccessor> failureMessages = failuresAccessor.GetFailureMessages();
				if (failureMessages.Count == 0)
				{
					e.SetProcessingResult((FailureProcessingResult)0);
					return;
				}
				if (transactionName.Equals(this.m_TransactionName))
				{
					foreach (FailureMessageAccessor failureMessageAccessor in failureMessages)
					{
						if ((int)failureMessageAccessor.GetSeverity() != 1)
						{
							e.SetProcessingResult((FailureProcessingResult)2);
							return;
						}
						failuresAccessor.DeleteWarning(failureMessageAccessor);
					}
					e.SetProcessingResult((FailureProcessingResult)1);
					return;
				}
				e.SetProcessingResult((FailureProcessingResult)0);
			}

			private string m_TransactionName = "";
		}
	}
}
